package com.linzelin.dao;

import com.linzelin.pojo.Department;
import com.linzelin.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDao {

    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;
    static{
        employees = new HashMap<>();

        employees.put(1001,new Employee(1001,"A","1990609801@qq.com",1,new Department(101,"数学部")));
        employees.put(1002,new Employee(1002,"B","1990609802@qq.com",1,new Department(102,"语文部")));
        employees.put(1003,new Employee(1003,"C","1990609803@qq.com",0,new Department(103,"英语部")));
        employees.put(1004,new Employee(1004,"D","1990609804@qq.com",0,new Department(104,"学习部")));
    }

    private static Integer initId = 1005;

    public void save(Employee employee){
        if (employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

        employees.put(employee.getId(),employee);

    }

    public Collection<Employee> getAll(){
        return employees.values();
    }

    public Employee getEmployee (Integer id){
        return employees.get(id);
    }

    public void delete(Integer id){
        employees.remove(id);
    }
}

